// pages/modifyshop/modifyshop.js
var app = getApp();
var util = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        image_all:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.getStoreInfo();
    },
    getStoreInfo:function(){
        var that = this;
        var that = this;
        util.showLoading();
        var apiUrl = '/api/store/store/index';
        var apiData = {};
        util.fetchPost(apiUrl,apiData,function(res){
            util.hideLoading();
            if(res.state == 1){
                var Things = res.datas.stalls_images;
                var id_str = res.datas.stalls_images_id;
                var image_all = [];
                for (var i = 0; i < Things.length; i++) {
                    image_all.push({'id':parseInt(id_str[i]),'path':Things[i]})
                };
                that.setData({
                    store_info:res.datas,
                    image_all: image_all,
                });
            }else{
                util.message(res.datas);
            }
        });
    },
    //图片上传
    uploadImg: function (e) {
        var that = this;
        wx.chooseImage({
            success(res) {
                util.showLoading();
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.http_url + '/api/v1/common/upload', //仅为示例，非真实的接口地址
                    header: {
                        'Content-Type': 'multipart/form-data'
                    },
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: app.globalData.key
                    },
                    success: function (result) {
                        util.hideLoading();
                        var image_all = that.data.image_all || [];
                        var image_path = that.data.image_path || [];
                        //上传图片后返回的是字符串，需json.prise一下
                        var res = JSON.parse(result.data);
                   
                        image_all.push(res);
                        image_path.push(res.path);
                        console.log(image_all);
                        that.setData({
                            image_all: image_all,
                            image_path: image_path,
                        });
                    },
                    fail: function (res) {
                        util.hideLoading();
                        util.message('上传服务器上传失败,请稍后再试2');
                        console.log(res);
                    }
                })
            }
        })
    },
    //删除评价图片
    onCloseImgTap: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var image_all = that.data.image_all;
        image_all.splice(index, 1)
        that.setData({
            image_all: image_all,
        });
    },
    formSubmit:function(e){
        var that = this;
        var store_introduce = e.detail.value.store_introduce;
        if(store_introduce.length < 1){
            util.showSuccess('请输入商家介绍','none',false,800);
            return false;
        }
        if(that.data.image_all.length < 1){
            util.showSuccess('请至少上传1张档口图片','none',false,800);
            return false;
        }
        var ppp = [];
        for (var i in that.data.image_all) {
            ppp.push(that.data.image_all[i].id);
        }
        console.log(ppp.join(','));
        util.showLoading();
        util.fetchPost("/api/store/store/store_modify", {
            store_introduce:store_introduce,
            stalls_images: ppp.join(','),
        }, function (res) {
            util.hideLoading();
            if (res.state == 1) {
                util.showSuccess(res.datas);
                setTimeout(function(){
                    wx.navigateBack();
                },2000);
            } else {
                util.message(res.datas)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})